package demo;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.nio.Buffer;
import java.nio.charset.StandardCharsets;
import java.util.Random;

public class TempCode {
    public static void main(String[] args) throws IOException {
        String file = "G:\\workspace\\javabasicproject\\src\\main\\java\\demo\\stack.log";
        String output = "G:\\javascript\\VueJs\\elementui-demo\\src\\mock\\stackContent.json";
        StringBuilder sb = new StringBuilder();
        try (InputStream inputStream = new FileInputStream(file);
             InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
             BufferedReader bReader = new BufferedReader(reader);) {
            String buf = null;
            while((buf = bReader.readLine())!=null) {
                sb.append(buf).append("\r\n");
            }
        }
        JSONObject json = new JSONObject();
        json.put("stack",sb.toString());
        FileOutputStream out = new FileOutputStream(output);
        OutputStreamWriter osw = new OutputStreamWriter(out,StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write(json.toJSONString());
        bw.flush();
        bw.close();
    }
}

class ThreadLocalIndex {
    private final ThreadLocal<Integer> threadLocalIndex = new ThreadLocal<Integer>();
    private final Random random = new Random();

    public int getAndIncrement() {
        Integer index = this.threadLocalIndex.get();
        if (null == index) {
            index = Math.abs(random.nextInt());
            this.threadLocalIndex.set(index);
        }

        index = Math.abs(index + 1);
        this.threadLocalIndex.set(index);
        return index;
    }

    @Override
    public String toString() {
        return "ThreadLocalIndex{" +
                "threadLocalIndex=" + threadLocalIndex.get() +
                '}';
    }
}
